<!-- 填写订单 -->
<!doctype html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
	<link rel="stylesheet" href="../res/css/weui.css" />
	<link rel="stylesheet" href="../res/css/weuix.css" />
	<link rel="stylesheet" href="../res/css/mp_common.css" />
	<link rel="stylesheet" href="../res/css/common.css" />
	<script src="../res/js/zepto.min.js"></script>
	<script src="../res/js/zepto.weui.js"></script>
	<script src="../res/js/mp_common.js?v=1"></script>
	<script src="../res/js/common.js"></script>
	<script type="text/javascript">
		var RxStateCode, RxStateName; //处方状态编码、处方状态名称（对应关系见方法getRxStateName）
	</script>

	<!-- 核销码 -->
	<!-- <script src="../res/js/verification.js"></script> -->
	<!-- <link rel="stylesheet" href="../res/css/verification.css" /> -->
	
	<style>
		.weui-cells {
			padding: 0px 10px;
			margin: 10px;
			background: #ffffff;
			position: relative;
			border-radius: 10px;
			font-size: 15px;
		}

		.weui-cell {
			padding: 10px 0;
			margin: 0;
			border-radius: 0px;
			border-bottom: 1px solid #efefef;
		}

		.weui-cell:last-child {
			border-bottom: unset;
		}

		#divShopList>div {
			background: white;
			border-radius: 5px;
			margin: 10px;
			padding: 5px 15px;
		}

		#divShopList>div:first-child {
			background: none;
			margin-left: 0px;
			padding-left: 2px;
			padding-top: 0px !important;
			margin-top: 0px;
		}

		.weui-table th,
		table td,
		table {
			font-size: 14px;
			padding: 4px 0px;
			border: 0px;
		}

		.weui-icon-radio {
			width: 14px;
			height: 14px;
			margin-top: 4px;
		}

		.weui-form-text p {
			font-size: 14px;
		}

		.clearfiex:before,
		.clearfiex:after {
			display: table;
			content: ".";
		}

		.clearfiex:after {
			clear: both;
		}

		.clearfiex {
			*zoom: 1;
		}

		table {
			border-radius: 5px;
		}

		table td {
			margin-top: 10px;
		}

		#divAddressList>table {
			border: none;
			background-color: white;
			margin: 10px;
			width: 95%;
		}

		.weui-cell_access .weui-cell__ft:after {
			margin-right: 14px;
		}

		.weui-form-checkbox[type="radio"]:checked+label .weui-icon-radio {
			background-color: #FCA92B;
			border: 1px solid #1189ff;
		}

		.show_drug {
			height: 60px;
			margin-top: 10px;
			display: flex;
		}

		.drug_img {
			width: 60px;
			height: 60px;
			/* float: left; */
			border-radius: 10px;
		}

		.show_drug>div {
			padding: 0 10px;
			color: #8a8a8a;
			/* float: left; */
			height: 60px;
			/* width: 70%; */
			line-height: 16px;
			position: relative;
		}

		.drug_price {
			/* position: absolute;
			top: 12px;
			right: 20px; */
			color: #FA6464;
		}

		.weui-pay-u li {
			line-height: 30px;
			justify-content: space-between;
		}

		.weui-pay-u .content,
		.weui-pay-u .title {
			font-size: 15px;
			width: unset;
			flex: unset;
		}

		.line {
			display: flex;
			font-size: 63px;
			height: 20px;
			line-height: 9px;
			color: #F9F8FE;
			overflow: hidden;
		}

		.line div {
			height: 18px;
			width: 18px;
			background: #F9F8FE;
			border-radius: 18px;
			margin: 0 1%;
			position: absolute;
		}

		.line div:first-child {
			left: -20px;
		}

		.line div:last-child {
			right: -20px;
		}

		.weui-popup__modal {
			background: #F9F8FE;
		}

		.address_td {
			width: 100px;
			text-align: center;
			padding-bottom: 4px;
		}

		.address_btn {
			background: #1189ff;
			color: #ffffff;
		}

		.weui-btn:after {
			border: 0px;
		}

		#btnBuyOrder {
			border-radius: 10px;
			background: #1189ff;
			color: #ffffff;
			width: 100%;
			height: 40px;
			line-height: 40px;
			font-size: 16px;
		}

		.weui-header .weui-header-left {
			left: 6px;
		}

		button.weui-btn_inline,
		input.weui-btn_inline,
		button.weui-btn_mini,
		input.weui-btn_mini {
			width: 72px;
		}

		.weui-btn {
			border-radius: 10px;
		}

		.deft {
			background-color: #ECF6FF;
			color: #1189FF;
			border-radius: 3px;
			display: inline-block;
			font-size: 10px;
			height: 18px;
			width: 40px;
			margin-left: 10px;
			text-align: center;
			line-height: 18px;
		}

		.weui-input {
			text-align: right;
			width: 90%;
			float: right;
			margin-right: 5px;
		}

		.select {
			outline: 1px solid #1189FF;
			border-radius: 10px;
		}

		.shopInput {
			height: 20px;
			width: 48%;
			margin-left: 10px;
			line-height: 30px;
			padding: 2px;
		}

		.shopListpicker {
			position: relative;
		}

		.dsp-flex{
			display: flex;
			justify-content: space-around;
		}

		.linTitle {
			margin-right: 5px;
			font-size: 13px;
		}

		#div_rx{
			overflow: hidden;
		}

		.drug_num{
			display: flex;
			/* flex-direction: column; */
			justify-content: space-between;
			flex-grow: 1;
		}
		
		.drug_num .drug_name{
			font-size: 14px;
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;
		}

		#div_medic .show_drug{
			margin: 0;
			padding: 10px 0;
		}
		.drug_num .drug_price{
			display: flex;
			align-items: center;
			flex-shrink: 0;
		}
		#div_medic .fu-shu{
			padding-bottom: 10px;
		}
		
		#inside_drug_house{
			display: none;
		}

		#saveHisOrder{
			border-radius: 10px;
			background: #1189ff;
			color: #ffffff;
			width: 100%;
			height: 40px;
			line-height: 40px;
			font-size: 16px;
			display: none;
		}
	</style>
</head>

<body ontouchstart>
	<div class="weui-header weui-btn_primary">
		<div class="weui-header-left"><a id="btnBack" class="icon icon-109"></a></div>
		<div class="weui-header-title">填写订单</div>
		<div class="weui-header-right"></div>
	</div>
	<div class="dt1_body">
		<div class="weui-cells weui-cells_form" pei>
			<div class="weui-cell">
				<div class="weui-cell__bd dsp-flex">
					<div class="iblock linTitle">请选择配送方式:</div>
					<div class="" id="div_kdps">
						<input class="weui-form-checkbox" name="txt_psfs" id="txt_psfs1" value="快递配送" type="radio"
							onclick="">
						<label for="txt_psfs1" onclick="courier()">
							<i class="weui-icon-radio"></i>
							<div class="weui-form-text" style="font-size: 14px;">
								<p>快递配送</p>
							</div>
						</label>
					</div>
					<div class="" id="div_smzq">
						<input class="weui-form-checkbox" name="txt_psfs" id="txt_psfs2" value="上门自取" type="radio"
							onclick="">
						<label for="txt_psfs2" onclick="invite()">
							<i class="weui-icon-radio"></i>
							<div class="weui-form-text" style="font-size: 14px;">
								<p>上门自取</p>
							</div>
						</label>
					</div>
				</div>
			</div>
			<!-- <div class="weui-cell chooseDecoction" style="display: none;">
				<div class="weui-cell__bd">
					<div class="iblock linTitle">是否代煎:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
					<div class="weui-form-li iblock" id="div_kdps">
						<input class="weui-form-checkbox" name="txt_doct" id="txt_doct1" value="快递配送" type="radio"
							onclick="">
						<label for="txt_doct1" onclick="">
							<i class="weui-icon-radio"></i>
							<div class="weui-form-text" style="font-size: 14px;">
								<p>是&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
							</div>
						</label>
					</div>
					<div class="weui-form-li iblock" id="div_smzq">
						<input class="weui-form-checkbox" name="txt_doct" id="txt_doct2" value="上门自取" type="radio"
							onclick="">
						<label for="txt_doct2" onclick="">
							<i class="weui-icon-radio"></i>
							<div class="weui-form-text" style="font-size: 14px;">
								<p>否</p>
							</div>
						</label>
					</div>
				</div>
			</div> -->
			<div class="weui-cell">
				<div class="weui-cell__bd" style="font-size: 15px; font-weight: bold;" id="RXnum">
					处方流水号201912122121
				</div>
			</div>
		</div>
		<div id="all_div">
			<div class="weui-cells weui-cells_form" pei>
				<div class="weui-cell" id="inside_drug_house">
					院内药房
				</div>
				<div class="weui-cell weui-cell_access open-popup" id="div_shop_address">
					<div class="weui-cell__bd" style="font-size: 15px; font-weight: bold;">
						<span style="vertical-align: middle; font-size: 15px;">请选择药店</span>
					</div>
					<div class="weui-cell__ft" style="font-size: 0">
						<span style="vertical-align: middle; font-size: 15px;"></span>
					</div>
				</div>
				<div class="weui-cell weui-cell_access open-popup" id="div_address">
					<div class="weui-cell__bd" style="font-size: 15px; font-weight: bold;">
						<span style="vertical-align: middle; font-size: 15px;">请选择收货地址</span>
					</div>
					<div class="weui-cell__ft" style="font-size: 0">
						<span style="vertical-align: middle; font-size: 15px;"></span>
					</div>
				</div>
				<div class="weui-cell" id="take_name">
					<div class="weui-cell__hd" style="font-size: 15px; font-weight: bold;">
						<span style="vertical-align: middle; font-size: 15px;">取件人姓名</span>
					</div>
					<div class="weui-cell__bd" id="nameInput">
						<input id="txt_name" class="weui-input" placeholder="请输入取件人姓名" type="text">
					</div>
				</div>
				<div class="weui-cell" id="take_phone">
					<div class="weui-cell__hd" style="font-size: 15px; font-weight: bold;">
						<span style="vertical-align: middle; font-size: 15px;">取件人电话</span>
					</div>
					<div class="weui-cell__bd" id="phoneInput">
						<input id="txt_phone" class="weui-input" placeholder="请输入取件人电话" type="text">
					</div>
				</div>
			</div>

			<div id="div_del_hidden_1">
				<div class="weui-cells">
					<div class="weui-cell">
						<div class="weui-cell__bd" id="div_rx">
							<div class="weui-cell weui-cell_access">
								<div class="weui-cell__bd" style="font-size: 15px; font-weight: bold;">
									药店
								</div>
								<!-- <div class="weui-cell__ft" style="font-size: 0">
									<span style="vertical-align: middle; font-size: 15px;"
										onclick="actToPage('shop_list.htm?act=sel')">
										选择药店
									</span>
								</div> -->
							</div>
						</div>
					</div>
					<!-- <div class="weui-cell" style="margin-top: -10px;" id="div_shop"></div> -->
				</div>
				<div class="weui-cells">
					<div id="div_medic"></div>
				</div>
				<div class="weui-pay weui-cells" id="div_price">
					<ul class="weui-pay-u">
						<li><span class="title">药品金额</span> <span class="content drug_Price"></span> </li>
						<li><span class="title" id="li_freight">配送费</span> <span class="content store_fee"></span> </li>
						<li><span class="title">开方时间</span> <span class="content creat_time"></span></li>
						<li><span class="title">合计金额</span> <span class="content total_Price"></span> </li>
					</ul>
				</div>
			</div>
		</div>
		<div id="div_save" style="margin: 15px 10px;">
			<a id="btnBuyOrder" href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary">提交订单</a>
		</div>
		<div style="margin: 15px 10px;">
			<div id="saveHisOrder" class="weui-btn weui-btn_mini weui-btn_primary">保存</div>
		</div>
		<div id="divpopup_1" class='weui-popup__container'>
			<div class="weui-popup__overlay">
			</div>
			<div class="weui-popup__modal">
				<div class="page-hd" style=" padding: 5px; background-color: #ffffff;">
					<h1 class="page-hd-title" style="font-size: 18px;">
						<span class="icon icon-109" style=" margin: 0px;" onclick="$('#a_close1').click();"></span>
						药店检索
					</h1>
				</div>
				<div>
				</div>
				<div class="weui-cells__title"
					style="display: flex;justify-content: space-between;align-items: center;">
					<div style="color: #353535;font-weight: bold;font-size: 18px;">药店列表</div>
				</div>
				<div style="width: 95%;display: flex;justify-content: space-between;">
					<input type="text" id="shopInput" oninput="shopValueChange()" placeholder="请输入药店名称"
						class="shopInput">
					<input type="text" class="shopListpicker shopInput" placeholder="请选择药店类型">
					<img src="../res/images/icon_put_away@2x.png"
						style="height: 25px;width: 25px;position: absolute;right: 18px;">
				</div>
				<div id="divShopList">
				</div>
				<a href="javascript:;" id="a_close1" class="weui-btn weui-btn_primary close-popup"
					style="display: none;">关闭</a>
			</div>
		</div>
		<div id="divpopup_2" class='weui-popup__container'>
			<div class="weui-popup__overlay"></div>
			<div class="weui-popup__modal">
				<!-- <div class="page-hd" style="margin-top: 5px;">
                <h1 class="page-hd-title" style="font-size: 26px; margin-top: -20px;">
                    <span class="icon icon-109" style="color: #07c160; margin: 0px;" onclick="$('#a_close1').click();"></span>
                    收货地址
                </h1>
            </div> -->
				<div class="weui-popup__modal">
					<div class="page-hd" style=" padding: 5px;background-color: #ffffff; ">
						<h1 class="page-hd-title" style="font-size: 18px;">
							<span class="icon icon-109" style=" margin: 0px;" onclick="$('#a_close1').click();"></span>
							收货地址
						</h1>
					</div>
					<div class="weui-cells__title" style=" color: black; font-size: 17px;">
						地址列表
					</div>
					<div id="divAddressList">
					</div>
					<div style="width:auto; height:40px; border-radius:10px; margin:20px; background-color:#1189ff; text-align:center; line-height:40px; color:#fff; z-index:999;"
						onclick="address_add()">
						新增收货地址
					</div>
					<a href="javascript:;" id="a_close2" class="weui-btn weui-btn_primary close-popup"
						style="display: none;">关闭</a>
				</div>
			</div>
		</div>
	</div>
</body>
<script type="text/javascript">
	strOrgId = getQst("orgid");
	strOpenId = getQst("openid");
	var user_id = getQst("user_id");
	var RXnum = getQst("prescription_id").trim(); //处方号
	var RXid = getQst("id").trim(); //处方号

	let type = 1;
	let index = 0;

	var addressId = ""; //地址id
	var deliveryMode = ""; //配送方式
	var total = ""; //总金额
	var orderMessage = ""; //备注
	var prescriptionId = ''; //处方id
	var storeId = ""; //药店id
	var storeName = "" //药店名称
	var orderGoodsList = '';
	var order_detail = [] //药店药品数组
	var shopKeyword = "" //药店搜索关键词
	var shopUse = "" //药店picker搜索
	
	let inside = getQst('inside')
	if(!inside || inside === 'false'){
		inside = false
	}else{
		inside = true
	}
	switchStateKey(inside)

	var price = 0; //单价
	//收货人
	var str_name = "";
	//收货人电话
	var str_photo = "";
	//收货地址
	var str_address = "";
	//详细地址
	var str_address_detl = "";
	//药店编码
	var group_no = "";
	//当前选择药店的运费
	var store_temp_freight = 0;
	//是否选择好药店和地址
	var showAddress = false,
		showDrugStore = false;
	//是否出现库存不足或者距离过远信息,不支持自提信息
	var outStockMessage = '',
		outDistanceMessage = '',
		noSelfMessage = ''
	//选择药店距离
	var group_distance = ""
	$(function () {

		// 购买方式选择
		// document.querySelector('#div_kdps').addEventListener('click',courier,true);
		// document.querySelector('#div_smzq').addEventListener('click',invite,true);
		$("#txt_psfs1").attr('checked', true);

		$("#RXnum").html("处方流水号：" + RXnum);
		var shop_no = sessionStorage.getItem("group_no");
		if(!inside){
			if (shop_no !== null) {
				showOrder(RXnum, shop_no);
			} else {
				showOrder(RXnum, "");
			}
		}

		//判断是否是从添加地址跳转
		var groupNo = getQst("group_no")
		if (groupNo) {
			group_distance = localStorage.getItem("storeDistance");
			console.log(group_distance);
			let params = {
				"prescriptionId": RXid
			};
			newAjaxPost("/h5/thirdParty/getShopList", params, (json) => {
				orderGoodsList = json.data.list;
				orderGoodsList.forEach((item, index) => {
					if (item.group_no == groupNo) {
						console.log(index);
						selShop(index)
					}
				})
			});
		}

		//$("#txt_psfs2").attr('disabled', true);
		//document.getElementById("all_div").style.display = "block";
		//document.getElementById("div_address").style.display = "block"  ///隐藏选择地址

		//打开页面默认输入
		// var shopChange = sessionStorage.getItem("shopStr");
		// var addressChange = sessionStorage.getItem("addressStr");
		// if( shopChange !== null){
		// 	$("#div_shop_address").html(shopChange);
		// }
		// if( addressChange !== null){
		// 	$("#div_address").html(addressChange);
		// }

		
		//返回上页
		$('#btnBack').on("click", function () {
			history.go(-1);
		});

		$('.outDistance').hide()

		if (isHis && inside) {
			$(".chooseDecoction").css({
				'display': 'block'
			});
			$("#txt_doct2").attr('checked', true);
			// 选中自提
			$("#txt_psfs1").attr('checked', false)
			$("#txt_psfs2").attr('checked', true)
			invite()
			// 隐藏快递
			$("#div_kdps").hide()
			getHisOrder()
			// 显示 药店 地图
			$("#inside_drug_house").css('display', 'flex')
			$("#div_shop_address").hide()

			// 点击 保存 按钮 提交院内订单
			$("#saveHisOrder").show().click(function () {
				// RXnum RXid
				let params = {
					id: RXid
				}
				newAjaxPost('/h5/lianjiang/his/addCflzHisPayMentInfo', params, function (res) {
					if(res.code == 1){
						showJSZ('提交订单成功')
						setTimeout(() => {
							actToPage(`user_dtl5.htm?prescription_id=${RXid}&user_id=${user_id}&inside=${inside}`)
						}, 1800);
					}
				})
			})
		}else{
			//新处方流转
			// 显示药店
			search_shop()
			// 地址选择
			get_address()
		}
	});

	// 获取院内订单详情
	function getHisOrder() {
		const params = { id: RXid }
		newAjaxPost("/h5/lianjiang/his/getPrescriptionInfoById", params, function(res){
			$("#nameInput").html(res.data.patientName).css('textAlign', 'right')
			$("#phoneInput").html(res.data.patientPhone).css('textAlign', 'right')
			
			$("#inside_drug_house").html(`
				<div>${res.data.hospitalName}</div>
				<div style="color: #10AEFF; margin-left: auto;" onclick="toMapPage(${res.data.lat}, ${res.data.lng})">查看地图></div>
			`)
			
			$("#div_save").hide()
			$("#div_del_hidden_1 > .weui-cells").eq(0).hide()
			// 药品金额
			$(".drug_Price").text(res.data.prescriptionPrice)
			// 开方时间
			$(".creat_time").text(res.data.prescriptionAuditedDate)

			$(".weui-pay-u > li:eq(3)").html(`<span class="title">合计(不含医保统筹/公医结账)</span> <span>￥${Number(res.data.prescriptionPrice).toFixed(2)}</span>`)

			// $(".weui-pay-u > li:eq(3)").after(`
			// 	<li style="justify-content:flex-end">
			// 		<span class="title" style="margin-right: 15px">总应付金额</span>
			// 		<span>￥${(res.data.prescriptionPrice - res.data.hisMiWholePrice).toFixed(2)}</span>
			// 	</li>
			// `)

			// 药品信息
			let medicineHtml = ''
			let fuShu = ''
			$(res.data.prescriptionInfoVOList).each(function (index, item) {
				// 付数
				fuShu = item.prescriptionNum ? `<div style="font-size:16px; font-weight:bold; padding-bottom: 8px;">付数：${item.prescriptionNum}付</div>` : ''
				medicineHtml += `
				<div class="show_drug">
					<img src=" ${isSpace(item.drugImg) ? '../res/images/medisen1.jpg' : item.drugImg}" class="drug_img">
					<div class="drug_num">
						<div class="drug_name">
							<div>${item.drugName}</div>
							<div style="font-size:12px;">
								<span>用量: ${item.detailsNum}${item.detailsConsumptionUnit}</span>
							</div>
						</div>
						<div class="drug_price" style="font-size:12px">
							￥<span class="unit_price">${item.drugPrice}</span>\/${item.drugPackagingUnit}
						</div>
					</div>
				</div>
				`
							// <span class="maign50">规格：${item.detailsConsumptionUnit}</span>
			})
			medicineHtml += fuShu
			medicineHtml += `<div style="font-size: 16px; font-weight:bold; padding-bottom:10px;">问诊人:${res.data.patientName}</div>`
			$("#div_medic").html(medicineHtml)
		})
	}

	// 查看地图
	function toMapPage(x, y) {
		actToPage(`user_dtl_map.htm?loc_x=${x}&loc_y=${y}`)
	}

	var timeFlag = true;

	function shopValueChange() {
		if (timeFlag) {
			timeFlag = false;
			var time = setTimeout(() => {
				shopKeyword = $('#shopInput').val()
				timeFlag = true;
				search_shop()
			}, 1000)
		}
	}

	$(".shopListpicker").picker({
		title: "请选择药店类型",
		cols: [{
			textAlign: 'center',
			values: ["可选药店", "不可选药店"]
		}],
		onChange: function (p, v, dv) {
			// console.log(p, v, dv);
		},
		onClose: function (p, v, d) {
			if (p.displayValue[0] == "可选药店") {
				shopUse = 2;
			} else {
				shopUse = 1;
			}
			search_shop()
		}
	});

	// var flag = false;  //选择药店按钮和删除按钮节流阀   true 可禁用 false 不能禁用
	var save_flag = false; //保存按钮节流阀

	function showOrder(order_id, group_no) {
		$.showLoading()
		// GetPageData('患者处方流水信息', { "actName": "actPatient", "actTag": "RxInformation", "prescription_id": order_id, "group_no": group_no }, showOrderList);
		paramJson = {
			"prescriptionNo": RXnum
		};
		newAjaxPost("/h5/myInterrogation/getPrescriptioninfo", paramJson, showOrderList);
	}

	//药品明细
	function showOrderList(rstData) {
		var str_rx_tab = "",
			str_pat = "",
			str_price_tab = "";
		if (rstData.code == 1) {
			prescriptionId = rstData.data.id
			str_rx_tab += '<table align="center" style="width: 100%;">';
			str_rx_tab += '<tr>';
			str_rx_tab += '<td colspan="2"><b style="color: #000; font-size: 16px;">问诊人：    ' + rstData.data.patientName +
				'</b></td>';
			str_rx_tab += '</tr>';
			str_rx_tab += '</table>';
			str_rx_tab += '<div class="line"><div></div>·························<div></div></div>';
			price = rstData.data.prescriptionPrice;
			if (rstData.data.westernMedicineResqList != null) {
				for (var i = 0; i < rstData.data.westernMedicineResqList.length; i++) {
					str_rx_tab += '<div class="show_drug">';
					str_rx_tab += '<img src="' + (isSpace(rstData.data.westernMedicineResqList[i].drugImg) ?
							"../res/images/medisen1.jpg" : rstData.data.westernMedicineResqList[i].drugImg) +
						'" class="drug_img">';
					str_rx_tab += '<div class="drug_num">';
					str_rx_tab += `<div class="drug_name">
						<div>${rstData.data.westernMedicineResqList[i].drugName}</div>
						<div style="font-size:12px">
							<span>用量：${rstData.data.westernMedicineResqList[i].detailsNum}  ${rstData.data.westernMedicineResqList[i].drugPackagingUnit} </span>
						</div>
					</div>`;
					str_rx_tab += `
						<div class="drug_price">￥
							<span class="unit_price">${rstData.data.westernMedicineResqList[i].drugPrice}</span>/
							${rstData.data.westernMedicineResqList[i].drugPackagingUnit}
						</div>
					`;
					// str_rx_tab += ' <span class="maign50">规格：' + rstData.data.westernMedicineResqList[i].drugSpecifications + '</span>';
					// str_rx_tab += '<div class="drug_price">￥<span class="unit_price">' + rstData.data
					// 	.westernMedicineResqList[i].drugPrice + '/' + '</span>/' +
					// 	rstData.data.westernMedicineResqList[i].drugPackagingUnit + '</div>';
					str_rx_tab += '</div>';
					str_rx_tab += '</div>';
				}
			} else {
				for (var i = 0; i < rstData.data.chineseMedicineResqList.length; i++) {
					str_rx_tab += '<div class="show_drug">';
					str_rx_tab += '<img src="' + (isSpace(rstData.data.chineseMedicineResqList[i].drugImg) ?
							"../res/images/medisen1.jpg" : rstData.data.chineseMedicineResqList[i].drugImg) +
						'" class="drug_img">';
					str_rx_tab += '<div class="drug_num">';
					str_rx_tab += `<div class="drug_name">
						<div>${rstData.data.chineseMedicineResqList[i].drugName}</div>
						<div style="font-size:12px">
							<span>用量：${rstData.data.chineseMedicineResqList[i].detailsConsumption} ${rstData.data.chineseMedicineResqList[i].detailsConsumptionUnit}</span>
						</div>
					</div>`;
					str_rx_tab += `
						<div class="drug_price">￥
							<span class="unit_price">${rstData.data.chineseMedicineResqList[i].drugPrice}</span>/${rstData.data.chineseMedicineResqList[i].drugPackagingUnit}
						</div>
					`;
					// + '<span style=\" margin-left:10px;\">规格：' + rstData.data.chineseMedicineResqList[i].drugSpecifications + '</span>';
					// str_rx_tab += '<div class="drug_price">￥<span class="unit_price">' + rstData.data
					// 	.chineseMedicineResqList[i].drugPrice + '</span>/' +
					// 	rstData.data.chineseMedicineResqList[i].drugPackagingUnit + '</div>';
					str_rx_tab += '</div>';
					str_rx_tab += '</div>';
				}
				str_rx_tab += '<div style="margin-top: 10px;">';
				str_rx_tab += '<span>付数:' + rstData.data.chineseMedicineResqList[0].prescriptionNum + '付</span>';
				str_rx_tab += '</div>';
			}

			ClickMe("txt_psfs", "快递配送");
			// $("#txt_psfs2").attr('disabled', true);
			$("#div_kdps").removeAttr("onclick");
			$("#div_smzq").removeAttr("onclick");
			courier();

			$("#div_rx").html(str_rx_tab);
			str_price_tab += '<ul class="weui-pay-u">';
			str_price_tab += '<li><span class="title">药品金额</span> <span class="content drug_Price">￥' + (Math.round(price *
				100) / 100) + '</span> </li>';
			// str_price_tab += '<li><span class="title">配送费</span> <span class="content store_fee">￥0</span> </li>';
			// if (isHis && inside) {
			// 	str_price_tab += '<li><span class="title">代煎费</span> <span class="content decoction">￥0</span> </li>';
			// }
			str_price_tab += '<li><span class="title">开方时间</span> <span class="content creat_time">' + rstData.data
				.createTime + '</span></li>';
			// if (isHis && inside) {
			// 	str_price_tab += '<li><span class="title">合计(不含医保/公医结账):</span> <span class="content total_Price">￥' + Number(price)
			// 		.toFixed(2) + '</span> </li>';
			// } else {
				str_price_tab += '<li><span class="title">合计金额</span> <span class="content total_Price">￥' + Number(price)
					.toFixed(2) + '</span> </li>';
			// }
			str_price_tab += '</ul>';
			$("#div_price").html(str_price_tab);

			//已选药店且没生成过订单 则可保存
			if (save_flag) {
				showJSZ("该订单已存在");
				//禁用选择收货地址
				$("#div_address").off('click')
				//禁用选择药店按钮
				$("#div_shop_address").off('click')
				//禁用保存按钮
				$("#div_save").html(
					'<a id="btnBuyOrder" href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary">提交订单</a>');
			} else {
				//生成保存按钮
				$("#div_save").click(shop_Save)
			}
			//            var take_type = $('input[name="txt_psfs"]:checked').val();
			//            if (take_type == "快递配送" && isSpace(rstData.rows[0].address_id)) {
			//                //禁用保存按钮
			//                $("#div_save").html('<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary" style="background-color: #B6B6B6; border-radius: 5px; float: right; color: White; margin-top: 15px; margin-right: 15px;">保存</a>');
			//            }
			// 未生成订单之前都可删除 和重选选择药店
			$("#div_delbtn").html('');
		} else if (rstData.total == 0) {
			$("#div_del_hidden_1").html("未找到该处方");
			$("#div_delbtn").html('');
			$("#div_save").html('');
		} else {
			showJSZ("程序错误请联系管理员");
			$("#div_del_hidden_1").html("未找到该处方");
			$("#div_delbtn").html('');
			$("#div_save").html('');
		}
		$.hideLoading()
	}

	//选择药店
	function selShop(index, obj) {
		var index = index
		search_shop()
		if (orderGoodsList[index].goods_check == 1) {
			msgInfo('所选药店缺少处方所需药品，请选择其他药店')
		} else if (orderGoodsList[index].goods_check == 2 && obj) {
			$.modal({
				title: "不同药房药品价格可能存在浮动，确定选择",
				buttons: [{
						text: "确定",
						onClick: function () {
							//index
							group_no = orderGoodsList[index].group_no
							//药店名称
							var group_name = orderGoodsList[index].group_name;

							storeName = group_name;
							//药店地址
							var group_address = orderGoodsList[index].group_address || '';
							//药店运费
							var store_temp_freight = orderGoodsList[index].store_temp_freight;

							console.log(document.querySelectorAll('.unit_price'))
							//药店对应药品信息
							order_detail = []
							orderGoodsList[index].order_detail[0].goods_detail.forEach((element,
								index) => {
								order_detail.push({
									"drugId": element.drug_id,
									"goodId": element.goods_id
								})
								//药品单价改变
								document.querySelectorAll('.unit_price')[index].innerText = element
									.goods_price
								// document.querySelector('.drug_Price').innerText = "￥" + element
								// 	.goods_price
								// document.querySelector('.total_Price').innerText = "￥" + element
								// 	.goods_price
							});
							console.log(order_detail);
							//药店距离
							group_distance = $(obj).attr("data-group-distance")
							console.log(group_distance)
							if (RXnum != null && RXnum != "") {
								var str_shop = "";
								str_shop +=
									'<div class="weui-cell" style="margin-top: 0px; border-bottom:0px; width:100%" id="div_shop">';
								str_shop +=
									'<div class="weui-cell__bd" id="shop_address_add" date-group_no="' +
									group_no + '" date-fee="' + store_temp_freight + '">';
								str_shop += '<div style="font-size:14px;">' + group_name;
								str_shop += Number(group_distance) ? `<span class="icon icon-69" style="font-size:14px;"></span> <span style="font-size:12px; color:Gray;">${group_distance}km</span>` : '';
								str_shop += '</div>';
								str_shop += '<div style="font-size:12px;color: #888888;">地址：' +
									group_address +
									'</div>';
								str_shop += '</div>';
								str_shop += '</div>';
								str_shop +=
									'<div class="weui-form-li iblock" style="margin:0px; width:10%; text-align:center;">';
								str_shop +=
									'<span class="icon icon-108" style="color: #07c160; margin: 0px;"></span>';
								str_shop += '</div>';

								$("#div_shop_address").html(str_shop);
								$('#a_close1').click();
								showDrugStore = true
								if (showDrugStore && showAddress) {
									whetherOrder(0)
								}
								// showOrder(RXnum, group_no);
							} else {
								showJSZ("程序错误");
							}
						}
					},
					{
						text: "取消",
						className: "default",
						onClick: function () {

						}
					},
				]
			});

		} else {
			group_no = orderGoodsList[index].group_no
			//药店名称
			var group_name = orderGoodsList[index].group_name;

			storeName = group_name;
			//药店地址
			var group_address = orderGoodsList[index].group_address;
			//药店运费
			var store_temp_freight = orderGoodsList[index].store_temp_freight;


			//药店对应药品信息
			order_detail = []
			orderGoodsList[index].order_detail[0].goods_detail.forEach((element,
				index) => {
				order_detail.push({
					"drugId": element.drug_id,
					"goodId": element.goods_id
				})
				//药品单价改变
				document.querySelectorAll('.unit_price')[index].innerText = element
					.goods_price
				// document.querySelector('.drug_Price').innerText = "￥" + element
				// 	.goods_price
				// document.querySelector('.total_Price').innerText = "￥" + element
				// 	.goods_price
			});
			console.log(order_detail);
			// //药店距离
			// if (obj) {
			// 	group_distance = $(obj).attr("data-group-distance")
			// } else {

			// }

			if (RXnum != null && RXnum != "") {
				var str_shop = "";
				str_shop +=
					'<div class="weui-cell" style="margin-top: 0px; border-bottom:0px; width:100%" id="div_shop">';
				str_shop +=
					'<div class="weui-cell__bd" id="shop_address_add" date-group_no="' +
					group_no + '" date-fee="' + store_temp_freight + '">';
				str_shop += '<div style="font-size:14px;">' + group_name;
				str_shop +=
					'<span class="icon icon-69" style="font-size:14px;"></span> <span style="font-size:12px; color:Gray;">' +
					group_distance + 'km</span>';
				str_shop += '</div>';
				str_shop += '<div style="font-size:12px;color: #888888;">地址：' +
					group_address +
					'</div>';
				str_shop += '</div>';
				str_shop += '</div>';
				str_shop +=
					'<div class="weui-form-li iblock" style="margin:0px; width:10%; text-align:center;">';
				str_shop +=
					'<span class="icon icon-108" style="color: #07c160; margin: 0px;"></span>';
				str_shop += '</div>';

				$("#div_shop_address").html(str_shop);
				// sessionStorage.setItem("shopStr",str_shop);
				// sessionStorage.setItem("group_no",group_no);
				$('#a_close1').click();
				showDrugStore = true
				if (showDrugStore && showAddress) {
					whetherOrder(0)
				}
				// showOrder(RXnum, group_no);
			} else {
				showJSZ("程序错误");
			}
		}
	}

	//保存(生成订单)
	function shop_Save() {
		var storeId = $("#shop_address_add").attr("date-group_no");
		var storeFee = $("#shop_address_add").attr("date-fee");
		var deliveryMode = $('input[name="txt_psfs"]:checked').val();
		var phone = '',
			userName = ''
		if (isEmpty(deliveryMode)) {
			showJSZ("请选择取货方式！");
			return false;
		}
		deliveryMode = (deliveryMode == "上门自取" ? "2" : "1");

		if (isEmpty(storeId)) {
			showJSZ("请选择药店！");
			return false;
		}
		if (deliveryMode == '1' && isEmpty(str_address)) {
			showJSZ("请选择收货地址！");
			return false;
		} else if (deliveryMode == '2') {
			addressId = '';
			phone = $('#txt_phone').val();
			userName = $('#txt_name').val();
			if (isEmpty(userName)) {
				showJSZ("请填写取件人姓名！");
				return false;
			} else if (isEmpty(phone)) {
				showJSZ("请填写取件人电话！");
				return false;
			} else if (!(/^1[3-9]\d{9}$/.test(phone))) {
				showJSZ("请正确填写取件人电话！");
				return false;
			}
		}
		if (outDistanceMessage) {
			msgInfo('该地址暂不支持配送，请更换地址')
		} else if (noSelfMessage) {
			msgInfo('该地址暂不支持自提，请更换地址')
		} else {
			$.confirm("保存后不能修改", "确认保存吗?", function () {
				paramJson = {
					"addressId": addressId, //地址id（自提时不填
					"deliveryMode": deliveryMode, //配送方式 1-物流配送，2-自提
					"money": total, //总金额(后台计算)
					"orderMessage": orderMessage, //订单备注
					"prescriptionId": prescriptionId, //处方id
					"storeId": storeId, //商店id
					"phone": phone, //取件人电话（物流配送时不填
					"userName": userName, //取件人姓名（物流配送时不填
					'type': 0, //为0是为创建订单,1不会创建
					"drugInfoList": order_detail,
					"storeName": storeName //药店名称
				}

				//禁用保存按钮
				$("#div_save").off()

				newAjaxPost("/h5/prescriptionOrder/getPrescriptionOrder", paramJson, function (rstData) {
					console.log(rstData)
					if (rstData.code == 1) {
						showJSZ("保存成功");
						// if(deliveryMode == '1') {		//物流配送
						//     actToPage('user_dtl5.htm?orgid=' + strOrgId + '&user_id=' + user_id + '&prescription_id=' + rstData.data.orderSn);
						// }else if(deliveryMode == '2'){	//自提
						// 	actToPage('user_dtl3.htm?orgid=' + strOrgId + '&user_id=' + user_id + '&prescription_id=' + rstData.data.orderSn);
						// }
						location.replace('user_dtl5.htm?orgid=' + strOrgId + '&user_id=' + user_id +
							'&prescription_id=' + rstData.data.orderSn);
						
						sessionStorage.removeItem("shopStr");
					} else {
						showJSZ(rstData.msg);
						$("#div_save").click(shop_Save)
					}
				});
			});

		}

	}


	// function indent_info_details() {
	//     GetPageData('生成订单详情表', { "actName": "actPatient", "actTag": "Save_indent_info_details", "prescription_id": RXnum }, function (rstData) {
	//         if (rstData.total > 0 && rstData.rows[0].msg == "成功") {
	//             actToPage('user_dtl5.htm?orgid=' + strOrgId + '&openid=' + strOpenId + '&prescription_id=' + RXnum);
	//             showJSZ("保存成功");
	//             //禁用保存按钮
	//             $("#div_save").html('<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary" style="background-color: #B6B6B6; border-radius: 5px; float: right; color: White; margin-top: 15px; margin-right: 15px;">保存</a>');
	//         }
	//         else if (rstData.total == 0) {
	//             showJSZ("保存失败");
	//         }
	//         else if (rstData.total < 0) {
	//             showJSZ("程序错误，请联系管理员");
	//         }
	//     });
	// }


	///快递配送
	function courier() {
		//document.getElementById("all_div_1").style.display = "none";
		document.getElementById("all_div").style.display = "block";
		document.getElementById("div_address").style.display = "flex" ///显示选择地址
		document.getElementById("take_name").style.display = "none"
		document.getElementById("take_phone").style.display = "none"
		setTimeout(() => {
			whetherOrder(1)
		}, 500);
		if (isHis && inside) {
			$(".chooseDecoction").css({
				'display': 'block'
			});
		}
	}

	///上门自取
	function invite() {
		// document.getElementById("all_div_1").style.display = "none";
		document.getElementById("all_div").style.display = "block";
		document.getElementById("div_address").style.display = "none"; ///隐藏选择地址
		document.getElementById("take_name").style.display = "flex";
		document.getElementById("take_phone").style.display = "flex";

		$("#li_freight").hide()
		if (isHis && inside) {
			$(".chooseDecoction").css({
				'display': 'none'
			});
		}
		setTimeout(() => {
			whetherOrder(1)
		}, 500);
	}

	//选择收货地址
	function check_address(obj) {
		$.modal({
			title: "确认选择当前收货地址吗",
			buttons: [{
					text: "确定",
					onClick: function () {
						//收货地址id
						addressId = $(obj).attr("data-id");
						//收货人
						str_name = $(obj).attr("data-pat_name");
						//收货人电话
						str_tel = $(obj).attr("data-dh");
						//收货地址
						str_address = $(obj).attr("data-address");
						//详细地址
						str_address_detl = $(obj).attr("data-address_detl");;
						if (RXnum != null && RXnum != "") {
							var address_div = "";
							address_div += '<table style="border:none;>';
							address_div += '<div class="address_add1" data-user_name="' + str_name +
								'" data-user_photo="' + str_tel + '" data-address="' + str_address +
								'" data-address_detl="' + str_address_detl + '"></div>';
							var reg = /^(\d{3})\d*(\d{4})$/;
							str_tel = str_tel.replace(reg, '$1****$2');
							address_div += '<tr>';
							address_div += '<td style="width:20%; color:black;">' + str_name + '</td>';
							address_div += '<td style="width:20%">' + str_tel + '</td>';
							address_div += '<td></td>';
							address_div +=
								'<td style="width:10%; text-align:center;" rowspan="2"><span class="icon icon-108" style="color: #07c160; margin: 0px;"></span></td>';
							address_div += '</tr>';
							address_div += '<tr>';
							address_div += '<td colspan="3">' + str_address + ' ' + str_address_detl +
								'</td>';
							address_div += '</tr>';
							address_div += '<tr class="outDistance">';
							address_div += '<td colspan="3" style="color: #FA6464;">该地址暂不支持配送，请更换地址</td>';
							address_div += '</tr>';
							address_div += '</table>';
							$("#div_address").html(address_div);
							sessionStorage.setItem("addressStr", address_div);
							$('#a_close2').click();
							showAddress = true
							$(".outDistance").hide()
							if (showAddress && showDrugStore) {
								whetherOrder(0)
							}
						}
					}
				},
				{
					text: "取消",
					className: "default",
					onClick: function () {

					}
				},
			]
		});
	}

	//药店检索
	function search_shop() {
		$.showLoading()
		let params = {
			"keyword": shopKeyword,
			"use": shopUse,
			"prescriptionId": RXid
		}
		newAjaxPost("/h5/thirdParty/getShopList", params, search_shopOK);
	}

	function search_shopOK(rstData) {
		// console.log(rstData);
		// H5 获取当前位置经纬度
		var location_lon = '',
			location_lat = ''; // 当前位置的经度,纬度

		//经纬度计算距离
		//lat经度  lng1纬度
		function getDistance(lat1, lng1, lat2, lng2) {
			lat1 = lat1 || 0;
			lng1 = lng1 || 0;
			lat2 = lat2 || 0;
			lng2 = lng2 || 0;

			var rad1 = lat1 * Math.PI / 180.0;
			var rad2 = lat2 * Math.PI / 180.0;
			var a = rad1 - rad2;
			var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
			var r = 6378137;
			sa2 = Math.sin(a / 2.0);
			sb2 = Math.sin(b / 2.0);
			d = 2 * r * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(rad1) * Math.cos(rad2) * sb2 * sb2));
			return (d / 1000);
		}
		if (navigator.geolocation) {

			navigator.geolocation.getCurrentPosition(function (position) {
				location_lon = position.coords.longitude;
				location_lat = position.coords.latitude;
				
			}, function (err) {
				console.log(err);
				// $("#divShopList").html(err.message)
			}), {
				timeout: 5000,
				enableHighAccuracy: true,
				maximumAge: 75000
			};
		} else {
			msgInfo("您的设备不支持定位功能");
		}
		if (rstData.code != 0) {
					var str_cells = "";
					str_cells += '<div class="weui-cell weui-cell_access" >';
					str_cells += '</div>';
					orderGoodsList = rstData.data.list
					for (var i = 0; i < rstData.data.list.length; i++) {
						//药店经纬度
						var doctorShop_lon = rstData.data.list[i].loc_y;
						// var doctorShop_lon = rstData.data.list[i].loc_x;
						var doctorShop_lat = rstData.data.list[i].loc_x;
						// var doctorShop_lat = rstData.data.list[i].loc_y;
						//获取距离
						var distance = getDistance(location_lon, location_lat, doctorShop_lon, doctorShop_lat)

						str_cells +=
							'<div class="weui-cell " style="height:80px;border-radius: 10px;" id="doctor_wrappar" data-index=' +
							(i + 1) + '>';
						str_cells +=
							`<div class="weui-cell__bd" data-group-distance=${distance.toFixed(2)}  onclick="selShop(${i},this)">`;
						str_cells += '<div style="font-size:14px;">' + rstData.data.list[i].group_name;
						str_cells += distance ?
							('<span class="icon icon-69" style="font-size:14px;"></span> <span style="font-size:12px; color:Gray;">' + distance.toFixed(2) + 'km</span>') : ''
						if (rstData.data.list[i].recommend == 1) {
							str_cells += `<span style="margin-left:20%;color: #1189FF;">推荐</span>`
						} else {

						}
						str_cells += '<div class="weui-form-li iblock" style="float:right; margin-right:10px;">';
						str_cells += '<input class="weui-form-checkbox" name="div_shop_list" id="txt_shop_list' +
							i + '" value="' +
							i + '" type="radio">';
						str_cells += '</div>';
						str_cells += '</div>';
						if (rstData.data.list[i].group_address) {
							str_cells +=
								'<div style="font-size:12px;color: #888888;margin-top:5px;margin-bottom:5px">地址：' +
								rstData.data.list[i].group_address + '</div>';
						} else {
							str_cells +=
								'<div style="font-size:12px;color: #888888;margin-top:5px;margin-bottom:5px">地址：</div>';
						}
						if (rstData.data.list[i].goods_check == 1) {
							str_cells +=
								`<div style="margin-top:5px;border-top:1px solid #E0E0E0"><span style="color:red">缺货：</span>${rstData.data.list[i].goods_str}</div>`
						} else {

						}
						str_cells += '</div>';
						str_cells += '</div>';
					}
					$("#divShopList").html(str_cells);
					// if()
				} else {
					$("#divShopList").html(
						'<div class="weui-cell weui-cell_access" style="background:#ffffff;margin:10px">无符合要求药店</div>'
					);
				}
		$.hideLoading()
	}
	// -----------------------

	//查询收货地址
	function get_address() {
		let params = {
			"current": 1,
			"size": 20
		}
		newAjaxPost("/h5/receivingAddress/getReceivingAddress", params, get_addressOk);
	}

	function get_addressOk(jsonInfo) {
		var data;
		console.log(1);
		if (jsonInfo.code == 1) {
			data = jsonInfo.data
			if (data.total > 0) { //有地址
				var dataArr;
				for (var j = 0; j < data.list.length; j++) {
					if (data.list[j].addressDeft == '1') {
						var deftData = data.list.splice(j, 1);
						dataArr = deftData.concat(data.list);
						showArr(dataArr)
						break
					} else {
						dataArr = data.list;
						showArr(dataArr)
					}
				}
			} else {
				dataArr = data.list;
				showArr(dataArr)
			}
		} else if (jsonInfo.code == 0) {
			$("#divAddressList").html("");
			showJSZ(jsonInfo.msg);
		} else if (jsonInfo.code < 0) {
			$("#divAddressList").html("");
			showJSZ("查询收货地址失败");
		}
	}

	function showArr(dataArr) {
		var address_div = "";
		for (var i = 0; i < dataArr.length; i++) {
			var str_tel = dataArr[i].userPhone;
			str_photo = dataArr[i].userPhone;
			var reg = /^(\d{3})\d*(\d{4})$/;
			str_tel = str_tel.replace(reg, '$1****$2');
			var str_address = dataArr[i].address + " " + dataArr[i].addressDetl;
			address_div += '<table style="border:none;   margin-top:15px;">';
			address_div += '<tr data-id="' + dataArr[i].id + '" data-pat_name="' + dataArr[i].userName + '" data-dh="' +
				dataArr[i].userPhone + '" data-address="' + dataArr[i].address + '" data-address_detl="' + dataArr[i]
				.addressDetl + '" onclick="check_address(this)">';
			address_div += '<td style="width:26% ; padding-left:10px; color: black;font-size: 16px;">' + dataArr[i]
				.userName + '</td>';
			if (dataArr[i].addressDeft == "1") {
				changeaddress(dataArr[i])
				address_div += '<td style="width:45%;font-size: 16px;">' + str_tel + '<span class="deft">默认</span></td>';
			} else {
				address_div += '<td style="width:45%;font-size: 16px;">' + str_tel + '</td>';
			}
			address_div += '<td></td>';
			address_div += '</tr>';
			address_div += '<tr data-id="' + dataArr[i].id + '" data-pat_name="' + dataArr[i].userName + '" data-dh="' +
				dataArr[i].userPhone + '" data-address="' + dataArr[i].address + '" data-address_detl="' + dataArr[i]
				.addressDetl + '" onclick="check_address(this)"><td colspan="3" style="padding-left:10px;">' +
				str_address + '</td></tr>';
			address_div += '<tr>';
			if (dataArr[i].addressDeft == "1") {
				address_div += '<td class="address_td" style="padding-left:10px;text-align:left"></td>';
			} else {
				address_div += '<td class="address_td" style="padding-left:10px;text-align:left"><a data-id="' + dataArr[i]
					.id + '" onclick="setDeft_address(this)" style="background-color: #fff;color:#757575;">设为默认</a></td>';
			}

			address_div +=
				'<td class="address_td" style="text-align:right"><button type="button" class="weui-btn weui-btn_mini address_btn" data-id="' +
				dataArr[i].id + '" data-deft="' + dataArr[i].addressDeft +
				'" onclick="delete_address(this)" style="background-color: #fff;color: #1189ff;border: 1px solid;">删除</button></td>';
			address_div +=
				'<td class="address_td" style="padding-left:20px;text-align:left"><button type="button" class="weui-btn weui-btn_mini address_btn" data-id="' +
				dataArr[i].id + '" onclick="change_address(this)">编辑</button></td>';
			address_div += '</tr>';
			address_div += '</table>';
		}
		$("#divAddressList").html(address_div);
	}
	// 自动选定默认地址
	function changeaddress(data) {
		//收货地址id
		addressId = data.id;
		str_name = data.userName;
		//收货人电话
		str_tel = data.userPhone;
		//收货地址
		str_address = data.address;
		//详细地址
		str_address_detl = data.addressDetl;
		if (RXnum != null && RXnum != "") {
			var address_div = "";
			address_div += '<table style="border:none;>';
			address_div += '<div class="address_add1" data-user_name="' + data.userName + '" data-user_photo="' + data
				.userPhone + '" data-address="' + data.address + '" data-address_detl="' + data.addressDetl + '"></div>';
			var reg = /^(\d{3})\d*(\d{4})$/;
			str_tel = data.userPhone.replace(reg, '$1****$2');
			address_div += '<tr>';
			address_div += '<td style="width:20%; color:black;">' + data.userName + '</td>';
			address_div += '<td style="width:20%">' + str_tel + '</td>';
			address_div += '<td></td>';
			address_div +=
				'<td style="width:10%; text-align:center;" rowspan="2"><span class="icon icon-108" style="color: #07c160; margin: 0px;"></span></td>';
			address_div += '</tr>';
			address_div += '<tr><td colspan="3">' + data.address + ' ' + data.addressDetl + '</td></tr>';
			address_div += '<tr class="outDistance">';
			address_div += '<td colspan="3" style="color: #FA6464;">该地址暂不支持配送，请更换地址</td>';
			address_div += '</tr>';
			address_div += '</table>';
			$("#div_address").html(address_div);
			$('#a_close2').click();
			showAddress = true
		}
	}
	//删除收货地址
	function delete_address(obj) {
		$.modal({
			title: "确认删除当前收货地址吗",
			buttons: [{
					text: "确定",
					onClick: function () {
						//地址id
						var address_id = $(obj).attr("data-id");
						var address_deft = $(obj).attr("data-deft");
						let params = {
							"id": address_id
						}
						newAjaxPost("/h5/receivingAddress/delReceivingAddress", params, delAddress);

						function delAddress(rstData) {
							if (rstData.code = 1) {
								if (address_deft == '1') { //删除的是默认地址
									var address_div = '';
									str_address = "";
									address_div +=
										'<div class="weui-cell__bd" style="font-size: 15px; font-weight: bold;">';
									address_div +=
										'	<span style="vertical-align: middle; font-size: 15px;">请选择收货地址</span>';
									address_div += '</div>';
									address_div += '<div class="weui-cell__ft" style="font-size: 0">';
									address_div +=
										'    <span style="vertical-align: middle; font-size: 15px;"></span>';
									address_div += '</div>';
									$("#div_address").html(address_div);
									get_address();
								} else {
									get_address();
								}
							} else {
								showJSZ(rstData.msg);
							}

						}
					}
				},
				{
					text: "取消",
					className: "default",
					onClick: function () {}
				},
			]
		});
	}
	//修改收货地址
	function change_address(obj) {
		//地址id
		var address_id = $(obj).attr("data-id");
		actToPage('user_address_change.htm?address_id=' + address_id + '&user_id=' + user_id);

	}
	//设置默认收货地址
	function setDeft_address(obj) {
		$.modal({
			title: "确认设置当前收货地址为默认地址吗",
			buttons: [{
					text: "确定",
					onClick: function () {
						//地址id
						var address_id = $(obj).attr("data-id");
						let params = {
							"id": address_id,
							"userId": user_id
						}
						newAjaxPost("/h5/receivingAddress/setDeftAddress", params, setDeft);

						function setDeft(rstData) {
							if (rstData.code = 1) {
								get_address();
							} else {
								showJSZ(rstData.msg);
							}

						}
					}
				},
				{
					text: "取消",
					className: "default",
					onClick: function () {}
				},
			]
		});
	}
	//新增收货地址
	function address_add() {
		localStorage.setItem("storeDistance", group_distance)
		actToPage('user_address_add.htm?orgid=' + strOrgId + '&openid=' + strOpenId + '&prescription_id=' + RXnum +
			'&user_id=' + user_id + '&id=' + RXid + '&group_no=' + group_no);
	}

	//判断药店库存和距离是否超过 参数isRadio是判断是否是通过上面radio按钮点击调用的
	function whetherOrder(isRadio) {
		storeId = $("#shop_address_add").attr("date-group_no");
		var deliveryMode = $('input[name="txt_psfs"]:checked').val();
		deliveryMode = (deliveryMode == "上门自取" ? "2" : "1");
		paramJson = {
			"addressId": addressId, //地址id（自提时不填
			"deliveryMode": deliveryMode, //配送方式 1-物流配送，2-自提
			"money": total, //总金额(后台计算)
			"orderMessage": orderMessage, //订单备注
			"prescriptionId": prescriptionId, //处方id
			"storeId": storeId, //商店id
			"phone": str_photo, //取件人电话（物流配送时不填
			"userName": str_name, //取件人姓名（物流配送时不填
			'type': 1, //为0是为创建订单,1不会创建
			'drugInfoList': order_detail,
			"storeName": storeName //药店名称
		}
		newAjaxPost("/h5/prescriptionOrder/getPrescriptionOrder", paramJson, function (rstData) {
			if (rstData.code == 0) {
				//判断是否是点击多选
				if (isRadio == 1) {
					if (rstData.msg.indexOf('不支持自提') > -1) {
						msgInfo(rstData.msg)
						noSelfMessage = rstData.msg
					} else {}
				} else {
					if (rstData.msg.indexOf('不支持配送') > -1) {
						msgInfo(rstData.msg)
						outDistanceMessage = rstData.msg
						$('.outDistance').show()
					} else {

					}
				}
			} else {
				if (deliveryMode == "2") {
					store_temp_freight = '￥' + rstData.data.shippingFee
					price = '￥' + Number(rstData.data.goodsAmount)
					total = Number(rstData.data.orderAmount)
				} else {
					if (rstData.data == null) {
						store_temp_freight = '￥' + store_temp_freight
						total = '￥' + (Number(price) + Number(store_temp_freight))
					} else {
						store_temp_freight = '￥' + rstData.data.shippingFee
						price = '￥' + Number(rstData.data.goodsAmount)
						total = Number(rstData.data.orderAmount)
					}
				}
				setTimeout(function () {
					$(".store_fee").html(store_temp_freight);
					$(".total_Price").html('￥' + Number(total).toFixed(2));
					$(".drug_Price").html(price)
				}, 100);
				outDistanceMessage = ""
				$('.outDistance').hide()
			}
		});
	}

	$("#div_shop_address").on('click',function () {
		$("#divpopup_1").popup()
	})

	$("#div_address").on('click',function () {
		$("#divpopup_2").popup()
	})
</script>

</html>